﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Roslyn.Compilers.CSharp;

namespace BITS.PreCompiler.CSharp.Syntax
{
    /// <summary>
    /// Represents a attribute abstraction.
    /// </summary>
    public partial class Attribute : BaseSyntaxNode<Attribute, AttributeSyntax>,
        IEnumerable<AttributeArgument>
    {
        # region • Fields & Properties •

        /// <summary>
        /// Holds the parent.
        /// </summary>
        private IBaseSyntaxNode parent;

        /// <summary>
        /// Gets the parent.
        /// </summary>
        public override IBaseSyntaxNode Parent
        {
            get { return this.parent; }
            internal set { this.parent = value; }
        }

        /// <summary>
        /// Holds the Attribute name
        /// </summary>
        public NameSyntax NameSyntax { get; set; }

        /// <summary>
        /// Gets the name.
        /// </summary>
        public String Name
        {
            get { return this.NameSyntax.ToString(); }
            set { this.NameSyntax = Roslyn.Compilers.CSharp.Syntax.ParseName(value); }
        }

        /// <summary>
        /// Holds the arguments
        /// </summary>
        private readonly List<AttributeArgument> arguments
            = new List<AttributeArgument>();

        /// <summary>
        /// Gets the attributes.
        /// </summary>
        public IEnumerable<AttributeArgument> Arguments
        {
            get { return this.arguments; }
        }

        /// <summary>
        /// Gets the argument list.
        /// </summary>
        private AttributeArgumentListSyntax ArgumentList
        {
            get
            {
                if (!this.Arguments.Any())
                    return null;

                return Roslyn.Compilers.CSharp.Syntax.AttributeArgumentList(
                    arguments: Roslyn.Compilers.CSharp.Syntax.SeparatedList(
                        nodes: this.Arguments.Select(x => x.GetSyntaxNode()),
                        separators: Enumerable.Repeat(Roslyn.Compilers.CSharp.Syntax.Token(
                            SyntaxKind.CommaToken), this.Arguments.Count() - 1)));

            }
        }

        # endregion Fields & Properties

        # region • Ctor's •

        /// <summary>
        /// Initializes a new instance of the <see cref="Attribute"/> attribute.
        /// </summary>
        internal Attribute() { }

        /// <summary>
        /// Initializes a new instance of the <see cref="Attribute"/> attribute.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public Attribute(IBaseSyntaxNode parent)
        {
            this.parent = parent;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Attribute"/> attribute.
        /// </summary>
        /// <param name="attribute">The attribute.</param>
        /// <param name="parent">The parent.</param>
        public Attribute(Attribute attribute, IBaseSyntaxNode parent = null)
            : this(attribute.GetSyntaxNode(), parent) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="Attribute"/> attribute.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="parent">The parent.</param>
        public Attribute(String name, IBaseSyntaxNode parent = null)
            : this(parent)
        {
            if (String.IsNullOrEmpty(name))
                throw new NullReferenceException("name");

            this.Name = name;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Attribute"/> attribute.
        /// </summary>
        /// <param name="syntax">The attribute declaration syntax.</param>
        /// <param name="parent">The parent.</param>
        public Attribute(AttributeSyntax syntax, IBaseSyntaxNode parent = null)
            : this(parent)
        {
            if (syntax == null)
                return;

            this.NameSyntax = syntax.Name;

            if (syntax.ArgumentList != null)
            {
                this.arguments = syntax.ArgumentList.Arguments
                    .Select(z => new AttributeArgument(z, this)).ToList();
            }
        }

        # endregion Ctor's

        # region • Methods •

        /// <summary>
        /// Builds the class decleration.
        /// </summary>
        /// <returns></returns>
        private AttributeSyntax BuildAttributeDecleration()
        {
            return Roslyn.Compilers.CSharp.Syntax.Attribute(
                name: this.NameSyntax,
                argumentList: this.ArgumentList);
        }

        public override AttributeSyntax GetSyntaxNode()
        {
            return this.BuildAttributeDecleration();
        }

        /// <summary>
        /// Adds the specified attribute argument.
        /// </summary>
        /// <param name="attributeArgument">The attribute argument.</param>
        public Attribute Add(AttributeArgument attributeArgument)
        {
            if (attributeArgument == null)
                return this;

            attributeArgument.Parent = this;
            this.arguments.Add(attributeArgument);

            return this;
        }

        # region • Methods - IEnumerable •

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> 
        /// that can be used to iterate through the collection.
        /// </returns>
        IEnumerator<AttributeArgument> IEnumerable<AttributeArgument>.GetEnumerator()
        {
            return this.Arguments.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.Arguments.GetEnumerator();
        }

        # endregion Methods - IEnumerable

        # endregion Methods
    }
}
